#!/usr/bin/perl
################################################################################
#
# Runs all jobControl files below the current directory.
#
#   usxRun.pl [logFile]
#
# This locates all *jobControl*.xml files below the current point, and runs
# all of them.  If one of them appears under UsfmInput, and if UsfmInput\input
# is non-empty, then we process that first.
#
# Note that the USFM analogue of this file used to run the proofer too,
# when required.  This file no longer does that, because the proofer was
# required for OliveTree only, and OliveTree now provide their own equivalent.
################################################################################

#-------------------------------------------------------------------------------
use strict;
use warnings;
use Cwd;
use Cwd 'abs_path';
use File::Basename;
use File::Copy;
use File::Find;



#-------------------------------------------------------------------------------
my $scriptFolder = dirname(File::Spec->rel2abs( __FILE__ ));
require "$scriptFolder/jamieSharedFunctions.pl";



#-------------------------------------------------------------------------------
my $topFolder = cwd;
my $javaFolder = File::Spec->catfile(abs_path("$scriptFolder/../_Java/"));
my $logFile = ""; if (scalar @ARGV > 0) { $logFile = File::Spec->catfile($ARGV[0]); }
my $teeScript = "\"$scriptFolder/tee.pl\"";
my $operatingSystem = getOperatingSystem();



#-------------------------------------------------------------------------------
if ("" ne $logFile)
{
  unlink($logFile);
  if ("Windows" eq $operatingSystem) { $logFile = "| perl $teeScript \"$logFile\""; }
  else { $logFile = "2>&1 | tee -a \"$logFile\""; }
}



#-------------------------------------------------------------------------------
# Delete anything in output folders, so that if the script should happen to
# fail, we aren't left with old files lying around.  Also, if we are generating
# USX from USFM, delete USX files.

deleteOutputFiles($topFolder);


#-------------------------------------------------------------------------------
my $filePattern = ".*JobControl.*.xml";
my @fileList;
find(\&getFiles, $topFolder);




#-------------------------------------------------------------------------------
# Carry out any conversion from USFM to USX first (if any).

foreach (@fileList) { if (-1 != index($_, "UsfmInput") && checkForUsfmInput($topFolder)) { invoke($_, "UsxConverter.jar"); } }
foreach (@fileList)
{
  if (-1 == index($_, "UsfmInput") && -1 == index($_, "OSIS") && -1 == index($_, "Proofer") && -1 == index($_, "UsfmOutput"))
  {
    invoke($_, "UsxConverter.jar");
  }
}



#-------------------------------------------------------------------------------
system("echo Processing completed successfully $logFile");



#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Recursively obtains all files matching filePattern, skipping subfolders whose
# names start "_".

sub getFiles
{
  my $file = $File::Find::name;
  if (-f $file && $file =~ /$filePattern/ && -1 == index($file, "/_")) { push(@fileList, $file); }
}    



#-------------------------------------------------------------------------------
# Invoke the relevant Java program in respect of the given XML file.

sub invoke
{
  my ($xmlFile, $jar) = @_;
  my $program = File::Spec->catfile($javaFolder, $jar);
  $xmlFile = File::Spec->catfile($xmlFile);
  
  system("echo Processing $xmlFile ... $logFile");
  my $status = system("java -jar \"$program\" \"$xmlFile\" $logFile");
  if (0 != ($status >> 8)) { die "Conversion failed"; }
  system("echo $xmlFile completed successfully ... $logFile");
}
